<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\controller\Config;
use think\facade\Cache;
use think\facade\View;
use app\common\model\Admin;
use addons\WeChatMini\facade\WeChatMini;
use app\common\tool\Result;

class Login
{


    public function index()
    {
        // try {
        //    //下单消息模板
        //     $minidata = [
        //         'touser' => 'okCyI5ct4RgUwQb90SkvFioNTG9k',
        //         'template_id' => '-MiWa4ixPv2cUyqH7zg99y6iKPcfXK84uoX8y9B3iS4',
        //         'page'  => 'pages/index/index',
        //         'miniprogram_state' => 'developer',
        //         'lang' => 'zh_CN',
        //         'data' => [
        //             'character_string1' => [
        //                 'value' => 'A15855624860000223545',
        //             ],
        //             'amount2' => [
        //                 'value' => '22.00',
        //             ],
        //             'thing6' => [
        //                 'value' => '张三',
        //             ],
        //             'time3' => [
        //                 'value' => date('Y-m-d H:i:s', time()),
        //             ],
        //             'phrase4' => [
        //                 'value' =>'成功',
        //             ]
        //         ]
        //     ];
        // //下单订阅消息接口
        //     $res = WeChatMini::wechatMiniTemplateSend($minidata);
        //     return Result::success('获取成功', $res);
        // } catch (\Exception $e) {
        //     return Result::fail($e->getMessage());
        // }
        //初始化系统参数
        if (!Cache::get('system')) {
            Cache::set('system', Config::lists());
        }
        View::assign('random', rand(1, 7));
        return View::fetch();
    }

    public function check(): \think\response\Json
    {
        return (new Admin())->login(input('post.'));
    }

    public function logout(): \think\response\Json
    {
        try {
            $admin_id = session('admin_id');

            $admin = Admin::find($admin_id);

            session('admin_id', null);
            session('password_token', null);
            \app\common\model\Logs::adds('管理员【' . $admin['admin_name'] . '】退出登录');

            return json(['code' => 1, 'msg' => '已退出']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }


    }

}
